import { login } from "@/api/login";
import { getopen } from "@/api/login";


export function xdLogin(data){
   return new Promise(function(resolve,reject){
        var loginform={
            openid: "",
            type: "mini",
            session_key: "",
            encryptData: "",
            iv: "",
            nickname: "",
            gender: "",
            avatar: "",
        }
    if (data.mp.detail.userInfo) {
        loginform.encryptData = data.mp.detail.encryptedData;
        loginform.iv = data.mp.detail.iv;
        loginform.nickname = data.mp.detail.userInfo.nickName;
        loginform.gender = data.mp.detail.userInfo.gender;
        loginform.avatar = data.mp.detail.userInfo.avatarUrl;
        wx.login({
        success: res => {
            getopen({
             code: res.code
            }).then(res => {
                loginform.openid = res.data.openid;
                loginform.session_key = res.data.session_key;
                login(loginform).then(res => {
                    // this.myinfo = res.data.userInfo;
                    // console.log(res.data)
                    if(res.code==200){
                        if(res.data.reg==0){
                            wx.setStorageSync("token", res.data.token);
                            wx.setStorageSync("user_info", res.data.userInfo);
                            let msg=res.msg
                            wx.showToast({
                                title: "登录成功",
                            });
                            resolve(res.data)
                        }else{
                            wx.navigateTo({
                                url: '/pages/tags/main?type=1',
                                success: (result) => {
                                    
                                },
                                fail: () => {},
                                complete: () => {}
                            });
                              
                        }
                        
                    }
                   
                }).catch(err=>{
                    reject(err)
                })
            });
        }
        });
    } else {
        console.log("请授权");
    }
   })
}
export default {
    xdLogin
}
  